package com.example.chatroom.controller;

import com.example.chatroom.common.AjaxResult;
import com.example.chatroom.entity.MessageEntity;
import com.example.chatroom.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

@RestController
public class MessageController {
    @Autowired
    private MessageService messageService;

    @RequestMapping("/getMessages")
    public AjaxResult getHistoryMessages(HttpServletRequest request,int sessionId){
        List<MessageEntity> messageList = messageService.getMessagesBySessionId(sessionId);
        // 在右侧消息栏里，消息是按照发送时间postTime进行升序排序（由上到下）
        Collections.reverse(messageList);
        return AjaxResult.success(messageList);
    }
}
